package com.zueb.service.impl;

import com.zueb.mapper.AcidMapper;
import com.zueb.pojo.NucleicAcid;
import com.zueb.service.NucleicAcidService;
import com.zueb.util.PageUtils;
import com.zueb.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class NucleicAcidServiceImpl implements NucleicAcidService {
    //1. 创建SqlSessionFactory 工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public List<NucleicAcid> showAcidByPage(int curPage, int pageSize, int facultyId, int classId) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        AcidMapper acidMapper = sqlSession.getMapper(AcidMapper.class);
        //调用方法
        int index = PageUtils.getIndex(curPage, pageSize);
        List<NucleicAcid> nucleicAcids = acidMapper.showAcidByPage(index, pageSize, facultyId, classId);
        //释放资源
        sqlSession.close();
        return nucleicAcids;
    }

    @Override
    public int showAcidCount(int facultyId, int classId) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        AcidMapper acidMapper = sqlSession.getMapper(AcidMapper.class);
        //调用方法
        int count = acidMapper.showAcidCount(facultyId, classId);
        //释放资源
        sqlSession.close();
        return count;
    }

    @Override
    public int addAcid(NucleicAcid nucleicAcid) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        AcidMapper acidMapper = sqlSession.getMapper(AcidMapper.class);
        //调用方法
        int flag = acidMapper.addAcid(nucleicAcid);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
        return flag;
    }

    @Override
    public List<NucleicAcid> toShowAcid(NucleicAcid nucleicAcid) {

        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        AcidMapper acidMapper = sqlSession.getMapper(AcidMapper.class);
        //调用方法
        List<NucleicAcid> nucleicAcids = acidMapper.toShowAcid(nucleicAcid);
        //释放资源
        sqlSession.close();

        return nucleicAcids;
    }


}
